package com.zzh.partnersys;

import com.google.gson.Gson;
import org.junit.jupiter.api.Test;
import org.redisson.api.GeoEntry;
import org.redisson.api.GeoUnit;
import org.redisson.api.RGeo;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.util.List;

@SpringBootTest
public class GeoTest2 {

    @Autowired
    private RedissonClient redissonClient;

    @Test
    public void testHyperLogLog() {
//        Jedis jedis = new Jedis("127.0.0.1", 6379);
//        Long findFriend = jedis.geoadd("findFriend", 121.470000, 31.230000, "1");
        RGeo<String> findFriendGeo = redissonClient.getGeo("partnerKing:findUser:location");
        findFriendGeo.add(new GeoEntry(121.470000, 31.230000, String.valueOf(10)));
        findFriendGeo.add(new GeoEntry(121.472800, 31.230000, String.valueOf(3)));
        findFriendGeo.add(new GeoEntry(116.400000, 39.900000, String.valueOf(5)));
        findFriendGeo.add(new GeoEntry(116.401500, 39.900000, String.valueOf(6)));
        findFriendGeo.add(new GeoEntry(113.140000, 23.080000, String.valueOf(1)));
        findFriendGeo.add(new GeoEntry(113.140800, 23.080000, String.valueOf(5003)));
        findFriendGeo.add(new GeoEntry(116.0119343,39.66127144, String.valueOf(10000)));
        findFriendGeo.add(new GeoEntry(116.7829835,40.2164962, String.valueOf(6004)));

        // 查找距离Palermo 200公里内的其他坐标
        List<String> user1IdList = findFriendGeo.radius("1", 1, GeoUnit.KILOMETERS);
        for (String s : user1IdList) {
            if (s.equals(String.valueOf(1))){
                System.out.println("Nearby location: " + s);//排除自己
            }
        }
        redissonClient.shutdown();

    }


    @Test
    public void search() {
        RGeo<String> findFriendGeo = redissonClient.getGeo("partnerKing:findUser:location");
        List<String> user1IdList = findFriendGeo.radius("1", 1, GeoUnit.KILOMETERS);
        for (String s : user1IdList) {
            if (!s.equals(String.valueOf(1))){
                System.out.println("Nearby location: " + s);//排除自己
            }
        }
        redissonClient.shutdown();
    }


}
